package com.alibaba.controller;


import com.alibaba.bean.PkR;
import com.alibaba.bean.PkResult;
import com.alibaba.bean.PkResult1;
import com.alibaba.bean.RankListData;
import com.alibaba.service.PkInterfaceService;
import com.alibaba.service.PkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("getpk")
public class PkInterfaceController {
    @Autowired
    PkInterfaceService pkInterfaceService;

    @Autowired
    PkService pkService;

    @RequestMapping(value = "pkinfo",method = RequestMethod.POST)
    @ResponseBody
    public PkResult Pkinfo(@RequestParam("hoursenum") int hoursenum){
        PkResult pkResult=new PkResult();
        PkResult1 pkResult1=new PkResult1();
        PkR rankListData1=new PkR();
        PkR rankListData2=new PkR();
        if(pkInterfaceService.selectRank1(pkInterfaceService.selectId1(hoursenum))!=null&&pkInterfaceService.selectRank2(pkInterfaceService.selectId2(hoursenum))!=null)
        {
            rankListData1=pkInterfaceService.selectRank1(pkInterfaceService.selectId1(hoursenum));
            rankListData2=pkInterfaceService.selectRank2(pkInterfaceService.selectId2(hoursenum));
            pkResult1.setUser1(rankListData1);
            pkResult1.setUser2(rankListData2);
            pkResult.setResult(true);
            pkResult.setErrorMsg("成功");
            pkResult.setData(pkResult1);
            pkInterfaceService.updatatimes(pkInterfaceService.selectId1(hoursenum));
            pkInterfaceService.updatatimes(pkInterfaceService.selectId2(hoursenum));
            if(pkInterfaceService.selecttimes(pkInterfaceService.selectId1(hoursenum))==2&&pkInterfaceService.selecttimes(pkInterfaceService.selectId2(hoursenum))==2){
                pkInterfaceService.deleteRank1(pkInterfaceService.selectId1(hoursenum));
                pkInterfaceService.deleteRank1(pkInterfaceService.selectId2(hoursenum));
                pkService.deletePk(hoursenum);
            }
            return pkResult;
        }else {
            pkResult.setResult(false);
            pkResult.setErrorMsg("失败");
            pkResult.setData(null);
            return pkResult;
        }

    }
}
